# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
from forecast_models import *
from accuracy import RMSE,MAE,MAPE,R_square,sMAPE
df = pd.read_csv('data.csv',index_col = 0)
df = df.interpolate(method='linear')
# df = df.iloc[0:3000,:]
import warnings
import time
times = time.strftime('%Y-%m-%d-%H-%M',time.localtime(time.time()))
# savepath = '预测结果保存'+'/'
filename = str(times)+ "预测值.csv"
# generpath(savepath)

warnings.filterwarnings('ignore')
horizon = 1
sequence_length = 12
epochs=25
#%% alo_lstm模型
model_index1 = 'alo-lstm'
y_test_rel,y_alo_pre = alo_lstm_model(df,sequence_length = sequence_length,horizon = horizon,epochs=epochs)
mae_alo = MAE(y_test_rel,y_alo_pre)
rmse_alo = RMSE(y_test_rel,y_alo_pre)
mape_alo = MAPE(y_test_rel,y_alo_pre)
R2_alo = R_square(y_test_rel,y_alo_pre)
print('alo的MAE为：'+str(mae_alo)) #"MAE:"
print('alo的MAPE为：'+str(mape_alo)) #"MAE:"
print('alo的RMSE为：'+str(rmse_alo)) #"RMSE:"
print('alo的R2为：'+str(R2_alo))
smape_alo=sMAPE(y_test_rel,y_alo_pre)
print('smape'+str(smape_alo))
#%% ga_lstm模型
model_index2 = 'ga-lstm'
y_test_rel,y_ga_pre = ga_lstm_model(df,sequence_length = sequence_length,horizon = horizon,epochs=epochs)
mae_ga = MAE(y_test_rel,y_ga_pre)
rmse_ga = RMSE(y_test_rel,y_ga_pre)
mape_ga = MAPE(y_test_rel,y_ga_pre)
R2_ga = R_square(y_test_rel,y_ga_pre)
print('ga的MAE为：'+str(mae_ga)) #"MAE:"
print('ga的RMSE为：'+str(rmse_ga)) #"RMSE:"
print('ga的MAPE为：'+str(mape_ga))
smape_ga=sMAPE(y_test_rel,y_ga_pre)
print('smape'+str(smape_ga))
# %% pso_lstm模型
# model_index3 = 'pso-lstm'
# y_test_rel,y_pso_pre = pso_lstm_model(df,sequence_length = sequence_length,horizon = horizon,epochs=epochs)
# mae_pso = MAE(y_test_rel,y_pso_pre)
# rmse_pso = RMSE(y_test_rel,y_pso_pre)
# mape_pso = MAPE(y_test_rel,y_pso_pre)
# R2_pso = R_square(y_test_rel,y_pso_pre)
# print('pso的MAE为：'+str(mae_pso)) #"MAE:"
# print('pso的RMSE为：'+str(rmse_pso)) #"RMSE:"
# print('pso的MAPE为：'+str(mape_pso))
# smape_pso=sMAPE(y_test_rel,y_pso_pre)
# print('smape'+str(smape_pso))
#%% 预测数据保存
model_index4 = 'ssa-lstm'
y_test_rel,y_ssa_pre = SSA(df,sequence_length = sequence_length,horizon = horizon,epochs=epochs)
mae_ssa = MAE(y_test_rel,y_ssa_pre)
rmse_ssa = RMSE(y_test_rel,y_ssa_pre)
mape_ssa = MAPE(y_test_rel,y_ssa_pre)
R2_ssa = R_square(y_test_rel,y_ssa_pre)
print('ssa的MAE为：'+str(mae_ssa)) #"MAE:"
print('ssa的RMSE为：'+str(rmse_ssa)) #"RMSE:"
print('ssa的MAPE为：'+str(mape_ssa))
smape_ssa=sMAPE(y_test_rel,y_ssa_pre)
print('smape'+str(smape_ssa))
print('******************')
y_total = np.hstack((y_test_rel,y_alo_pre))
y_total = np.hstack((y_total,y_ga_pre))
# y_total = np.hstack((y_total,y_pso_pre))
y_total = np.hstack((y_total,y_ssa_pre))
# y_total = np.hstack((y_total,y_svr_pre))
y_t = pd.DataFrame(y_total)
y_t.columns=['y_true','y_alo_pre','y_ga_pre','y_ssa_pre']
ttx = 'optimized-lstm-result2.csv'
y_t.to_csv(ttx)
#%% 误差数据保存
ind1 = np.array([mae_alo ,rmse_alo ,mape_alo,R2_alo,smape_alo]).reshape(-1,1)
ind2 = np.array([mae_ga ,rmse_ga ,mape_ga,R2_ga,smape_ga]).reshape(-1,1)
# ind3 = np.array([mae_pso ,rmse_pso ,mape_pso,R2_pso,smape_pso]).reshape(-1,1)
# ind4 = np.array([mae_svr ,rmse_svr ,mape_svr]).reshape(-1,1)
ind4 = np.array([mae_ssa ,rmse_ssa ,mape_ssa,R2_ssa,smape_ssa]).reshape(-1,1)
ind = np.hstack((ind1,ind2))
# ind = np.hstack((ind,ind3))
ind = np.hstack((ind,ind4))
indx = pd.DataFrame(ind)
indx.columns=['alo-lstm','ga-lstm','ssa-lstm']
indx.index = ['MAE','RMSE','MAPE','R2','SMAPE']
tt = 'optimized-lstm-index2.csv'
indx.to_csv(tt)